package com.ronsai.umtrack.util.permission;

import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;

import com.ronsai.umtrack.R;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

import java.util.List;

public class PermissionUtil {

    public static void requestPermission(final Context context, final PermissionCallback callback, final String... permissions) {
        AndPermission.with(context)
                .runtime()
                .permission(permissions)
                .rationale(new DefaultRationale())
                .onGranted(permissions1 -> {
                    if (callback != null) callback.onGranted();
                })
                .onDenied(permissionList -> {
                    if (AndPermission.hasAlwaysDeniedPermission(context, permissionList)) {
                        List<String> permissionNames = Permission.transformText(context, permissionList);
                        String message = context.getString(R.string.message_permission_always_failed, TextUtils.join("\n", permissionNames));

                        new AlertDialog.Builder(context)
                                .setCancelable(false)
                                .setTitle(R.string.title_dialog)
                                .setMessage(message)
                                .setPositiveButton(R.string.setting, (dialog, which) -> AndPermission.with(context)
                                        .runtime()
                                        .setting()
                                        .onComeback(() -> {
                                            if (callback != null) {
                                                if (AndPermission.hasPermissions(context, permissions)) {
                                                    callback.onGranted();
                                                } else {
                                                    callback.onDenied();
                                                }
                                            }
                                        })
                                        .start())
                                .setNegativeButton(R.string.cancel, (dialog, which) -> {
                                    if (callback != null) callback.onDenied();
                                })
                                .show();
                    } else {
                        if (callback != null) callback.onDenied();
                    }
                })
                .start();
    }

    private void requestPermission(final Context context, final PermissionCallback callback, final String[]... permissions) {
        AndPermission.with(context)
                .runtime()
                .permission(permissions)
                .rationale(new DefaultRationale())
                .onGranted(permissions1 -> {
                    if (callback != null) callback.onGranted();
                })
                .onDenied(permissionList -> {
                    if (AndPermission.hasAlwaysDeniedPermission(context, permissionList)) {
                        List<String> permissionNames = Permission.transformText(context, permissionList);
                        String message = context.getString(R.string.message_permission_always_failed, TextUtils.join("\n", permissionNames));

                        new AlertDialog.Builder(context)
                                .setCancelable(false)
                                .setTitle(R.string.title_dialog)
                                .setMessage(message)
                                .setPositiveButton(R.string.setting, (dialog, which) -> AndPermission.with(context)
                                        .runtime()
                                        .setting()
                                        .onComeback(() -> {
                                            if (callback != null) {
                                                if (AndPermission.hasPermissions(context, permissions)) {
                                                    callback.onGranted();
                                                } else {
                                                    callback.onDenied();
                                                }
                                            }
                                        })
                                        .start())
                                .setNegativeButton(R.string.cancel, (dialog, which) -> {
                                    if (callback != null) callback.onDenied();
                                })
                                .show();
                    } else {
                        if (callback != null) callback.onDenied();
                    }
                })
                .start();
    }

}
